import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

const double $ToolbarHeight = 44;

AppBar xAppBar(
  context,
  title, {
  action,
  TextStyle? style,
  Widget? leading,
  PreferredSizeWidget? bottom,
  bool centerTitle = true,
  Color? bgColor,
  bool automaticallyImplyLeading = true,
  double titleSpacing = NavigationToolbar.kMiddleSpacing,
  double elevation = 0.0,
}) {
  List<Widget>? actions;

  if (action is List) {
    actions = [...action, SizedBox(width: 8)];
  } else if (action is Widget) {
    actions = [action, SizedBox(width: 8)];
  }

  Widget? _title, _flexibleSpace;

  if (title is String) {
    _title = Text(
      title,
      style: style ?? const TextStyle(fontSize: 17),
    );
  } else if (title is Widget) {
    _title = title;
  } else {
    assert(title == null);
  }

  final currentState = Get.key.currentState?.canPop() ?? false;
  if (currentState && leading == null) {
    leading = xBackBtn(
        color: Theme.of(context).brightness == Brightness.dark
            ? Colors.white
            : Colors.black);

    titleSpacing = 0;
  } else if (leading is SizedBox) {
    titleSpacing = 0;
    leading = const SizedBox();
  } else {
    titleSpacing = titleSpacing;
  }

  return AppBar(
    leading: leading,
    centerTitle: centerTitle,
    title: _title,
    flexibleSpace: _flexibleSpace,
    actions: actions,
    bottom: bottom,
    titleSpacing: titleSpacing,
    backgroundColor: bgColor,
    elevation: elevation,
    automaticallyImplyLeading: automaticallyImplyLeading,
    // systemOverlayStyle:
    //     const SystemUiOverlayStyle(statusBarIconBrightness: Brightness.dark),
    scrolledUnderElevation: 0.0,
    shape: const Border(
      bottom: BorderSide(
        color: Colors.transparent, // 设置为透明以移除下划线
      ),
    ),
  );
}

Widget xBackBtn({Color? color}) {
  return IconButton(
    iconSize: 20,
    icon: Icon(Icons.arrow_back_ios_new, color: color),
    onPressed: () {
      Get.back();
    },
  );
}
